var express = require("express");
var path = require("path");
var cookieParser = require("cookie-parser");
var logger = require("morgan");

// 使用中间件
const adminAuth = require("./middlewares/admin-auth");
const visitLoggerMiddleware = require("./middlewares/visit-logger-middleware");

// 引入 express-session 中间件
const session = require("express-session");

require("dotenv").config();

// 清理日志
const { clearVisitLog } = require("./utils/logger");

// 解决跨域问题
const cors = require("cors");

// 路由文件
var indexRouter = require("./routes/index");
var usersRouter = require("./routes/users");
// 管理员设置路由文件
const adminSettingsRouter = require("./routes/admin/settings");
// 管理员用户路由文件
const adminUsersRouter = require("./routes/admin/user");
// 登录路由文件
const adminAuthRouter = require("./routes/admin/auth");
// 验证码路由文件
const captchaRouter = require("./routes/admin/captcha");
// 访问日志路由文件
const visitLogRouter = require("./routes/admin/visitLogs");
// 尺码规则路由文件
const sizeRuleRouter = require("./routes/admin/sizeRule");
// 下载尺码规则路由文件
const downloadSizeRuleRouter = require("./routes/admin/downloadSizeRule");
// 消息路由文件
const messageRouter = require("./routes/admin/message");
// ip配置路由文件
const ipConfigRouter = require("./routes/admin/ipConfig");
// 产品路由文件
const productionRouter = require("./routes/admin/production");
// 码垛线请求
const palletizingRouter = require("./routes/admin/palletizing");

// 模拟授权
const simulationRouter = require("./routes/api/auth");
// 码垛线反馈
const palletizingFeedbackRouter = require("./routes/api/palletizing");

var app = express();

app.use(logger("dev"));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, "public")));
// 解决跨域
app.use(cors());
// 访问日志记录
//app.use(visitLoggerMiddleware);

// 配置 express-session 中间件
app.use(
  session({
    secret: process.env.SESSION_SECRET || "fallback_secret", // 确保 secret 选项已设置
    resave: false,
    saveUninitialized: true,

    cookie: { secure: process.env.NODE_ENV === "production" },
  })
);

app.use("/", indexRouter);
app.use("/users", adminAuth, usersRouter);
app.use("/admin/settings", adminAuth, adminSettingsRouter);
app.use("/admin/users", adminAuth, adminUsersRouter);
app.use("/admin/auth", adminAuthRouter);
app.use("/admin/captcha", captchaRouter);
app.use("/admin/logs", adminAuth, visitLogRouter);
app.use("/admin/sizeRule", adminAuth, sizeRuleRouter);
app.use("/admin/downloadSizeRule", adminAuth, downloadSizeRuleRouter);
app.use("/admin/message", adminAuth, messageRouter);
app.use("/admin/ipConfig", adminAuth, ipConfigRouter);
app.use("/admin/production", adminAuth, productionRouter);
app.use("/admin/palletizing", adminAuth, palletizingRouter);
// 模拟授权
app.use("/api/auth", simulationRouter);
app.use("/api/palletizing", palletizingFeedbackRouter);

// 清理日志
clearVisitLog();

// 在项目入口文件顶部添加全局异常处理
process.on("uncaughtException", (err) => {
  console.error("未捕获的异常:", err);
  // 不要退出进程，记录错误并继续运行
});

process.on("unhandledRejection", (reason, promise) => {
  console.error("未处理的Promise拒绝:", reason);
  // 不要退出进程，记录错误并继续运行
});

module.exports = app;
